package isg3.data;

import isg3.domain.*;
import isg3.utils.UIDGenerator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class JDBCLibrosPorEventoDAO implements ILibrosPorEventoDAO {
private Connection conn;

public JDBCLibrosPorEventoDAO(){
	conn = ConnectionManager.getInstance().checkOut();
	System.out.println("SE HA CREADO LA CONEXION");
}

	public List selectAllLibros(String OIDEvento) {
		PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM  libros INNER JOIN librosporevento ON libros.OIDLibro = librosporevento.libroOID WHERE (eventoOID = ?)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.setString(1, OIDEvento);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Libro temp = new Libro();
                temp.setTitulo(result.getString("titulo"));
                temp.setAutor(result.getString("autor"));
                temp.setEditorial(result.getString("editorial"));
                temp.setCategoria(result.getString("categoria"));
                temp.setISBN(result.getString("ISBN"));
                temp.setIdLibro(result.getString("libroOID"));
                
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
    }
	
	public void insert(String idE, String idL){
		String oido = UIDGenerator.getInstance().getKey();
        PreparedStatement stmt = null;
        String sql = "INSERT INTO librosporevento ( OIDLpe, eventoOID, libroOID) VALUES (?, ?, ?) ";
        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, oido);
            stmt.setString(2, idE);
            stmt.setString(3, idL);
            
    

            stmt.executeUpdate();


        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}


}
